from rest_framework import serializers
from goods.models import SKUImage, SKU
from fdfs_client.client import Fdfs_client
from django.conf import settings

# conn = Fdfs_client(conf_path='./meiduo_mall/utils/fastdfs/client.conf')
# conn = Fdfs_client(conf_path=settings.FDFS_CONF_PATH)
# # res = conn.upload_by_filename('./jwt_token.py')
# res = conn.upload_by_buffer(f.read())
# res['Status'] == 'Upload successed'

class ImageSerializer(serializers.ModelSerializer):
    sku = serializers.PrimaryKeyRelatedField(queryset=SKU.objects.all())

    class Meta:
        model = SKUImage
        fields = [
            'id',
            'sku',
            'image'
        ]

    # def validate(self, attrs):
    #     file_obj = attrs['image']
    #     conn = Fdfs_client(settings.FDFS_CONF_PATH)
    #     res = conn.upload_by_buffer(file_obj.read())
    #     if res['Status'] != 'Upload successed':
    #         raise serializers.ValidationError('文件上传fdfs失败')
    #     attrs['image'] = res['Remote_file_id']
    #     return attrs

class SKUSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'id', 'name'
        ]
